package behaviourNavigatore;

import java.awt.Dimension;
import java.util.List;

import org.jgrapht.alg.DijkstraShortestPath;

import utility.Auto;
import utility.MyDefaultWeightedEdge;
import utility.Triple;

import navigatore.AgenteNavigatore;

import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;


public class GenerateRoute extends OneShotBehaviour{

	private static final long serialVersionUID = 1L;

	private AgenteNavigatore navigatore;
	
	@Override
	public void action() {
		
		//System.out.println("GENERATE ROUTE!!!");

		navigatore = (AgenteNavigatore)myAgent;
		
		
		List<Auto> listCars1 = navigatore.getWaitingCarsWithoutWays();
		List<Auto> listCars2 = navigatore.getWaitingCarsWithWays();

		if(!listCars1.isEmpty()){
			
			DijkstraShortestPath<Triple<Dimension, String, String>, MyDefaultWeightedEdge> path;
			Triple<Dimension, String, String> partenza, destinazione;


			while(!listCars1.isEmpty()) {

				Auto auto = listCars1.get(0);

				
				partenza = auto.getStart();
				destinazione = auto.getEnd();

				path = new DijkstraShortestPath<Triple<Dimension,String,String>, MyDefaultWeightedEdge>(navigatore.getGraph(), partenza, destinazione);
				auto.setWay(path.getPathEdgeList());
				
				auto.calcolaVelocitaStepIdeali();
				
				//auto.setWay(DijkstraShortestPath.findPathBetween(navigatore.getGraph(), partenza, destinazione));
				
				auto.setStreetStart(auto.getWay().get(0).getStreetName());
				auto.setStreetEnd(auto.getWay().get(auto.getWay().size()-1).getStreetName());
				
				listCars2.add(auto);
				listCars1.remove(0);

			}

		}

	}
	
	@Override
	public int onEnd() {
		if(!navigatore.isActive())
			return navigatore.FINALSTATE;
		else 
			return navigatore.GENROUTE_TO_PUTINSTREET;
		
	}

}